package com.zhengshang.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;

/**
 * @author HY
 * @version 1.0.0
 * @ClassName PartnerLevelUtils.java
 * @Description TODO
 * @createTime 2021年05月08日 20:11
 */
public class PartnerLevelUtils {

    //贡献值
    private final static int PARTNER_0 = 0;
    private final static int PARTNER_1 = 1;
    private final static int PARTNER_2 = 50;
    private final static int PARTNER_3 = 200;
    private final static int PARTNER_4 = 500;
    private final static int PARTNER_5 = 1000;
    private final static int PARTNER_6 = 2000;
    private final static int PARTNER_7 = 5000;
    private final static int PARTNER_8 = 10000;
    private final static int PARTNER_9 = 20000;

    private static JSONArray contributionValueArr = null;
    private final static int[] partners = {PARTNER_0, PARTNER_1, PARTNER_2, PARTNER_3, PARTNER_4, PARTNER_5, PARTNER_6, PARTNER_7, PARTNER_8, PARTNER_9};
    private final static int[] rewards = {0, 1000, 5000, 30000, 60000, 100000, 200000, 300000, 400000};

    /**
     * @title 获取合伙人等级
     */
    public static int getLevel(BigDecimal contributionValue){
        int level = 0;
        if (contributionValue.compareTo(new BigDecimal(PARTNER_1)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_2)) == -1) {
            level = 1;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_2)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_3)) == -1) {
            level = 2;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_3)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_4)) == -1) {
            level = 3;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_4)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_5)) == -1) {
            level = 4;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_5)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_6)) == -1) {
            level = 5;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_6)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_7)) == -1) {
            level = 6;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_7)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_8)) == -1) {
            level = 7;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_8)) > -1  && contributionValue.compareTo(new BigDecimal(PARTNER_9)) == -1) {
            level = 8;
        } else if (contributionValue.compareTo(new BigDecimal(PARTNER_9)) == 1) {
            level = 9;
        }
        return level;
    }

    /**
     * @title 获取合伙人等级奖励
     */
    public static BigDecimal partnerReward(int contributionValue){
        switch (contributionValue) {
            case PARTNER_2:
                return new BigDecimal(rewards[1]);
            case PARTNER_3:
                return new BigDecimal(rewards[2]);
            case PARTNER_4:
                return new BigDecimal(rewards[3]);
            case PARTNER_5:
                return new BigDecimal(rewards[4]);
            case PARTNER_6:
                return new BigDecimal(rewards[5]);
            case PARTNER_7:
                return new BigDecimal(rewards[6]);
            case PARTNER_8:
                return new BigDecimal(rewards[7]);
            case PARTNER_9:
                return new BigDecimal(rewards[8]);
            default:
                return new BigDecimal(rewards[0]);
        }
    }

    /**
     * @title 获取合伙人等级奖励
     */
    public static BigDecimal partnerReward(BigDecimal value){
        int contributionValue = value.intValue();
        return partnerReward(contributionValue);
    }

    /**
     * @title 获取等级规则
     */
    public static JSONArray getSection(){
        if (contributionValueArr == null) {
            contributionValueArr = new JSONArray();
            for (int i = 0; i < partners.length; i++) {
                JSONObject object = new JSONObject();
                object.put("level", i);
                object.put("minContribution", partners[i]);
                if (i < partners.length - 1) {
                    object.put("maxContribution", partners[i + 1] - 0.1);
                } else {
                    object.put("maxContribution", -1);
                }
                contributionValueArr.add(object);
            }
        }
        return contributionValueArr;
    }
}
